//
//  Parser.m
//  CEDict
//
//  Created by Eric Nadeau on 07-06-02.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "Parser.h"


@implementation Parser

+(Entry*) parseLine: (NSString*) line
{
	NSScanner* scanner = [NSScanner scannerWithString:line];
	NSString* traditional;
	NSString* simplified;
	NSString* pinyin;
	NSMutableArray* english = [NSMutableArray array];
	
	// Read the traditional characters
	[scanner scanUpToString:@" " intoString:&traditional];

	// Skip space between traditional and simplified characters
	while([scanner scanString:@" " intoString:nil]);
	
	// Read the simplified characters
	[scanner scanUpToString:@" " intoString:&simplified];

	// Move to pinyin
	[scanner scanUpToString:@"[" intoString:nil];
	
	// Skip opening bracket before pinyin
	[scanner scanString:@"[" intoString:nil];
	
	// Read pinyin
	[scanner scanUpToString:@"]" intoString:&pinyin];
	
	// Move to English definitions
	[scanner scanUpToString:@"/" intoString:nil];

	// Skip first slash so that definitions are all in the .../ format
	[scanner scanString:@"/" intoString:nil];
	
	NSString* definition;
	// Read definition, which ends at slash
	while( [scanner scanUpToString:@"/" intoString:&definition] )
	{
		// Add definition to list of definitions
		[english addObject:definition];
		
		// Skip trailing slash after definition
		[scanner scanString:@"/" intoString:nil];
	}
	
	Entry* retval = [[Entry alloc] 
		initWithSimplifed:simplified 
		   andTraditional:traditional 
				andPinyin:pinyin 
			   andEnglish:english
		];
	
	return retval;
}

@end
